package model.impl.menu;

import model.EMenuState;
import model.impl.StateListener;
import sharedGameLogic.players.Character;
import util.CharacterLoadException;
import util.CharacterLoader;

public class CharacterSelectionState extends AbstractMenuState {
	private static final CharacterSelectionState INSTANCE = new CharacterSelectionState();
	
	private static final int ALTERNATIVE_BACK = 0;
	private static final int ALTERNATIVE_PLAY = 1;
	
	private int chosenCharacter;

	private CharacterSelectionState() {
		currentAlternative = 0;
		int numberOfCharacters = 0;;
		try {
			numberOfCharacters = CharacterLoader.getNumberCharacters();
		} catch (CharacterLoadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		numberOfAlternatives = 2 + numberOfCharacters;
		setInitialChoosenCharacter();
	}
	
	public static CharacterSelectionState getInstance() {
		return INSTANCE;
	}
	
	@Override
	public void fireAlternative() {
		if (currentAlternative > 1) {
			chosenCharacter = currentAlternative;
			updateCharacter();
		} else if (currentAlternative == ALTERNATIVE_BACK) {
			viewMainMenu();
		} else if (currentAlternative == ALTERNATIVE_PLAY) {
			if (chosenCharacter != 0) {
				updateCharacter();
				startGame();
			}
		}
	}

	private void updateCharacter() {
		Character selectedCharacter = null;
		try {
			selectedCharacter = CharacterLoader.getModelCharacters().get(chosenCharacter - 2);
		} catch (CharacterLoadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (StateListener listener : listeners) {
			listener.selectedCharacterChanged(selectedCharacter);
		}
	}
	
	private void startGame() {
		for (StateListener listener : listeners) {
			listener.startGame();
		}
	}

	@Override
	public int getChosenCharacterIndex() {
		return chosenCharacter;
	}

	@Override
	public String getIP() {
		return null;
	}

	@Override
	public String getNick() {
		return null;
	}

	@Override
	public boolean isFullScreen() {
		return false;
	}

	private void viewMainMenu() {
		for (StateListener listener : listeners) {
			listener.viewMainMenu();
		}
	}

	//This method is only supposed to be used in the SettingsState
	@Override
	public void insertCharacter(char c) {
		
	}

	//This method is only supposed to be used in the SettingsState
	@Override
	public void removeCharacter() {
		
	}

	@Override
	public EMenuState getEMenuState() {
		return EMenuState.CHARACTER_SELECTION;
	}

	@Override
	public void moveAlternativeLeft() {
		if (currentAlternative == ALTERNATIVE_BACK) {
			currentAlternative = ALTERNATIVE_PLAY;
		} else if (currentAlternative == ALTERNATIVE_PLAY) {
			currentAlternative = ALTERNATIVE_BACK;
		}
	}

	@Override
	public void moveAlternativeRight() {
		if (currentAlternative == ALTERNATIVE_BACK) {
			currentAlternative = ALTERNATIVE_PLAY;
		} else if (currentAlternative == ALTERNATIVE_PLAY) {
			currentAlternative = ALTERNATIVE_BACK;
		}
	}
	
	@Override
	public void resetMarkedAlternativePosition() {
		super.resetMarkedAlternativePosition();
		setInitialChoosenCharacter();
	}
	
	private void setInitialChoosenCharacter() {
		if (numberOfAlternatives > 2) {
			chosenCharacter = 2;
		}
	}
}
